using System;
using System.ComponentModel;
using Microsoft.Practices.WizardFramework;
using VSSDKAssist.Assets.Converters;
using VSSDKAssist.Recipes.Common.CustomPages;
using VSSDKAssist.Recipes.Common.Helpers;
using VSSDKAssist.Recipes.Common.Editors.Grid;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;

namespace VSSDKAssist.Recipes.CustomProject.CreateBitmapStrip
{
	/// <summary>
    /// Custom page for CreateBitmapStripCP recipe
	/// </summary>
	public partial class CreateBitmapStripCP2 : WizardPageBase
	{
        private DataGridViewBinding<IconInfo> binding;

		/// <summary>
		/// Initializes a new instance of the <see cref="CreateBitmapStripCP2"/> class.
		/// </summary>
		public CreateBitmapStripCP2()
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="CreateBitmapStripCP2"/> class.
		/// </summary>
		/// <param name="wizard">The <see cref="T:Microsoft.Practices.WizardFramework.WizardForm"/>that will host the new page.</param>
        public CreateBitmapStripCP2(WizardForm wizard)
			: base(wizard)
		{
			InitializeComponent();
            binding = new DataGridViewBinding<IconInfo>();
            binding.DataChanged += delegate { Wizard.OnValidationStateChanged(this); };
		}

        private List<IconInfo> iconInfoList;

        [RecipeArgument]
        public List<IconInfo> IconInfoList
        {
            get { return iconInfoList; }
            set
            {
                iconInfoList = value;
                binding.Bind(gridIconInfo, iconInfoList);
            }
        }

		/// <summary>
		/// 	<see cref="P:Microsoft.WizardFramework.WizardPage.IsDataValid"/>
		/// Search for all the arguments that this Wizard gather
		/// If all of them are not null, then the Wizard can finish
		/// </summary>
		/// <value></value>
		public override bool IsDataValid
		{
            get
            {
                if(iconInfoList != null)
                {
                    bool isValid = true;

                    IBindingStrategy bindingStrategy = new ListBindingStrategy(iconInfoList);
                    isValid &= Validation.ExecuteConfigurationValidatorAttributes(gridIconInfo, Site, bindingStrategy);

                    isValid &= CheckFileNames(gridIconInfo, columnIcon);

                    if(!isValid)
                    {
                        return false;
                    }
                }

                return base.IsDataValid;
            }
		}

		/// <summary>
		/// Gets or sets the site of the control.
		/// </summary>
		/// <value></value>
		/// <returns>The <see cref="T:System.ComponentModel.ISite"></see> associated with the <see cref="T:System.Windows.Forms.Control"></see>, if any.</returns>
		/// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
		public override ISite Site
		{
			get
			{
				return base.Site;
			}
			set
			{
				base.Site = value;
				if(value != null)
				{
                    gridIconInfo.Site = new SiteProxy(this.Site);
				}
			}
		}

        public static bool CheckFileNames(DataGridView grid, DataGridViewColumn column)
        {
            bool isValid = true;

            foreach(DataGridViewRow row in grid.Rows)
            {
                string rowValue = row.Cells[column.Index].Value as string;

                if(rowValue != null)
                {
                    if(!File.Exists(rowValue))
                    {
                        Validation.AppendErrorText(row.Cells[column.Index], Properties.Resources.FileDoesntExist);
                        isValid = false;
                    }
                    else
                    {
                        FileInfo info = new FileInfo(rowValue);

                        if(info.Extension != Properties.Resources.IcoExtension)
                        {
                            Validation.AppendErrorText(row.Cells[column.Index], Properties.Resources.InvalidValue);
                            isValid = false;
                        }
                    }
                }
            }

            return isValid;
        }
	}
}